<!DOCTYPE html>  
<html>
    <meta charset="utf-8" />  
    <title>双链表</title>  
<body>
    <h2>双链表</h2>
</body>
<script>  
    // class Node{
    //     constructor(element){
    //         this.element = element;
    //         this.prev = null;
    //         this.next = null;
    //     }
    // }

    // class DoublyLinkedList{
    //     constructor(){
    //         this.head = null;
    //         this.tail = null;
    //         this.length = 0;
    //     }

    //     // 插入
    //     insert(position,element){
    //         if(position>=0&&position<=this.length){
    //             const node = new Node(element);
    //             let current = this.head;
    //             let previous = null;
    //             let index = 0;
    //             if(position == 0){
    //                 if(!this.head){
    //                     this.head = node;
    //                     this.tail = node;
    //                 }else{
    //                     node.next = current;
    //                     current.prev = node
    //                     this.head = node;
    //                 }
    //             }else if(position === this.length){
    //                 current = this.tail;
    //                 current.next = node;
    //                 node.prev = current;
    //                 this.tail = node; 
    //             }else{
    //                 while (index++ < position) {
    //                     previous = current
    //                     current = current.next
    //                 }
    //                 node.next = current
    //                 previous.next = node
    //                 current.prev = node
    //                 node.prev = previous
    //             }
    //             this.length++
    //             return true
    //         }
    //         return false
    //     }


    //     // 移除指定位置元素
    //     removeAt(position) {
    //         if (position > -1 && position < this.length) {
    //             let current = this.head
    //             let previous = null
    //             let index = 0

    //             // 首位
    //             if (position === 0) {
    //                 this.head = this.head.next
    //                 this.head.prev = null
    //                 if (this.length === 1) {
    //                     this.tail = null
    //                 }

    //             // 末位
    //             } else if (position === this.length - 1) {
    //                 this.tail = this.tail.prev
    //                 this.tail.next = null

    //             // 中位
    //             } else {
    //                 while (index++ < position) {
    //                     previous = current
    //                     current = current.next
    //                 }
    //                 previous.next = current.next
    //                 current.next.prev = previous
    //         }
    //         this.length--
    //         return current.element
    //         } else {
    //             return null
    //         }
    //     }
    // }

    // const doublyLinkedList = new DoublyLinkedList();

    
    // doublyLinkedList.insert(0,'a')
    // // doublyLinkedList.insert(0,'m')
    // // doublyLinkedList.insert(0,'p')
    // doublyLinkedList.insert(1,'b')
    // doublyLinkedList.insert(1,'f')
    // // doublyLinkedList.insert(2,'c')

    // doublyLinkedList.removeAt(0)
    
    // console.log(doublyLinkedList.head)

    class Node{
        constructor(element){
            this.element = element;
            this.prev = null;
            this.next = null;
        }
    }

    class DoublyLinkedList{
        constructor(){
            this.head = null;
            this.tail = null;
            this.length = 0;
        }
        insert(position,element){
            if(position>-1&&position<=this.length){
                const node = new Node(element);
                let current = this.head;
                let index = 0;
                let previous = null;
                if(position === 0){
                    if(this.head === null){
                        this.head = node;
                        this.tail = node;
                    }else{
                        node.next = current;
                        current.prev = node;
                        this.head = node;
                    }
                }else if(position==this.length){
                    current = this.tail;
                    current.next = node;
                    node.prev = current;
                    this.tail = node;
                }else{
                    while(index++<position){
                        previous = current;
                        current = current.next;
                    }
                    node.next = current
                    previous.next = node
                    current.prev = node
                    node.prev = previous
                }
                this.length++;
                return true;
            }
            return false;
        }
    }

    const doublyLinkedList = new DoublyLinkedList();

    doublyLinkedList.insert(0,'a');
    doublyLinkedList.insert(0,'b');
    doublyLinkedList.insert(0,'c');
    doublyLinkedList.insert(3,'d');
    doublyLinkedList.insert(1,'1');
    console.log('head',doublyLinkedList.head)
    console.log('length',doublyLinkedList.length)
</script>  
</html>